//
//  HomeViewController.swift
//  weiboDemo
//
//  Created by zq on 2020/10/27.
//

import UIKit

private let cellId = "cellId"

class HomeViewController: BaseViewController {

    private lazy var statusList = [String]()
    
    override func loadData() {
        // 模拟延迟刷新
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
            if self.isPullUp { //加载更多
                for i in 0..<10 {
                    self.statusList.append("加载更多:\(i.description)")
                }
                self.isPullUp = false
            }else{//刷新
                self.statusList.removeAll()
                for i in 0..<18 {
                    self.statusList.insert("刷新 \(i.description)", at: 0)
                }
            }
            self.refreshControl?.endRefreshing()
            self.tableView?.reloadData()
        }
    }
    
    @objc private func showFriends(){
        let vc = DemoViewController()
        vc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(vc, animated: true)
    }

}

extension HomeViewController{
    
    override func setupUI() {
        super.setupUI()
        navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "好友", target: self, action: #selector(showFriends))
        //注册cellid
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
}

extension HomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //去除cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        //设置cell
        cell.textLabel?.text = statusList[indexPath.row]
        //返回cell
        return cell
    }
}
